package android.slc.sys.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.slc.appbase.ui.activity.base.AppMvvmBaseActivity;
import android.slc.appbase.vm.AppBaseViewModel;
import android.slc.appbase.vm.GlobalDataVm;
import android.slc.attachment.bean.Progress;
import android.slc.extras.component.attchment.utils.AttachmentConfig;
import android.slc.extras.system.config.ConstantsSystem;
import android.slc.extras.system.domain.SystemVmBox;
import android.slc.extras.system.entify.AppVersions;
import android.slc.network.SimpleDownloadByOk;
import android.slc.network.SimpleDownloadByOkListener;
import android.slc.sys.R;
import android.slc.widget.RectProgressView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.PathUtils;
import com.google.android.material.button.MaterialButton;

import java.io.File;
import java.text.DecimalFormat;

public class UpDataActivity extends AppMvvmBaseActivity<ViewDataBinding, AppBaseViewModel> implements View.OnClickListener {
    private final DecimalFormat df = new DecimalFormat("#.##");
    private TextView tv_title;
    private LinearLayout negative_parent;
    private MaterialButton btn_negative;
    private RectProgressView btn_positive;
    private TextView tv_msg;
    private AppVersions upDate;
    private int state;
    private File appFile;

    public static void showUpDataActivity(Context context, AppVersions tbAppVersions) {
        Intent intent = new Intent(context, UpDataActivity.class);
        intent.putExtra(ConstantsSystem.Key.KEY_UP_DATE, tbAppVersions);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public Object setContentView() {
        return R.layout.sys_dialog_update;
    }

    @Override
    public void onBindViewBefore() {
        super.onBindViewBefore();
        GlobalDataVm.getInstance().systemVmBox.updateHandlerStatusOf.set(SystemVmBox.UPDATE_STATUS_NONE);
    }

    @Override
    public void onBindView(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onBindView(savedInstanceState);
        setFinishOnTouchOutside(false);
        negative_parent = findViewById(R.id.negative_parent);
        btn_negative = findViewById(R.id.btn_negative);
        btn_negative.setOnClickListener(this);
        btn_positive = findViewById(R.id.btn_positive);
        btn_positive.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_msg = findViewById(R.id.tv_msg);

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        layoutParams.width = screenWidth - screenWidth / 6;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onBindViewLater() {
        upDate = (AppVersions) getIntent().getSerializableExtra(ConstantsSystem.Key.KEY_UP_DATE);
        if (upDate.isForceUpdate()) {
            negative_parent.setVisibility(View.GONE);
        }
        tv_title.setText(R.string.title_the_latest_version);
        tv_msg.setText(upDate.getUpdateContent().replace("\\n", "\n"));
        super.onBindViewLater();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_negative) {
            super.onBackPressed();
            GlobalDataVm.getInstance().systemVmBox.updateHandlerStatusOf.set(SystemVmBox.UPDATE_STATUS_SKIP);
        } else if (id == R.id.btn_positive) {
            switch (state) {
                case Progress.NONE:
                    negative_parent.setVisibility(View.GONE);
                    tv_title.setText(R.string.title_be_updating);
                    btn_positive.setText(getString(R.string.action_get_the_installation_package));
                    checkUpDate();
                    break;
                case Progress.FINISH:
                    AppUtils.installApp(appFile);
                    break;
            }
        }
    }

    private void checkUpDate() {
        String downloadPath = AttachmentConfig.getInstance().explicitDownloadPathAuto(upDate.getUrl());
        SimpleDownloadByOk.newBuilder(downloadPath, PathUtils.getExternalAppDownloadPath(), "app-release.apk")
                .toCommonlyConfig()
                .build()
                .getDownloadTask().enqueue(new SimpleDownloadByOkListener() {
            @Override
            protected void started(@NonNull com.liulishuo.okdownload.DownloadTask task) {
            }

            @Override
            protected void progress(@NonNull com.liulishuo.okdownload.DownloadTask task, int percentage, long currentOffset, long totalLength) {
                state = Progress.LOADING;
                btn_positive.setProgress(percentage);
                btn_positive.setText(getString(R.string.label_completed_size, df.format(percentage) + "%"));
            }

            @Override
            protected void completed(@NonNull com.liulishuo.okdownload.DownloadTask task) {
                state = Progress.FINISH;
                appFile = task.getFile();
                btn_positive.setProgress(btn_positive.getMax());
                btn_positive.setText(getString(R.string.action_install_now));
                AppUtils.installApp(appFile);
            }

            @Override
            protected void error(@NonNull com.liulishuo.okdownload.DownloadTask task, @NonNull Exception e) {
                state = Progress.NONE;
                btn_positive.setProgress(-1);
                btn_positive.setText(getString(R.string.action_download_on_error_click_retry));
            }

        });
    }

    @Override
    public void onBackPressed() {
        if (upDate == null || !upDate.isForceUpdate() && state != Progress.LOADING) {
            super.onBackPressed();
        }
    }
}
